// pages/admin/home/home.js
var dateTimePicker = require('../../../utils/dateTimePicker.js');


Page({

  /**
   * 页面的初始数据
   */
  data: {
    oder_Lists_total: 0,
    color: "#ff6f10",
    disabled: false,
    getCode: "获取验证码",
    order_content_lists: [],
    order_content: "",
    is_boss: true,
    is_admin: true,
    oder_lists_print: [],
    time: '12:01',
    //date: '2018-12-25',
    date: '2018-10-01',
    time: '12:00',
    dateTimeArray: null,
    dateTime: null,
    dateTimeArray1: null,
    dateTime1: null,
    dateTimeArray2: null,
    dateTime2: null,
    startYear: 2000,
    endYear: 2050,
    index: 0,
    index_type: 0,
    index_status: 0,
    //index: null,
    picker: ['全部', '已支付', '待支付', '已配送'],
    picker_type: ['全部', '邮寄', '代取'],
    order_status_max: 9999,
    order_status_min: -9999,
    order_type: ".",
  },
  sendCode: function (e) {
    var that = this;
    var times = 0
    var i = setInterval(function () {
      times++
      if (times >= 60) {
        that.setData({
          color: "#ff6f10",
          disabled: false,
          getCode: "获取验证码",
        })
        clearInterval(i)
      } else {
        that.setData({
          getCode: "重新获取" + times + "s",
          color: "#999",
          disabled: true
        })
      }
    }, 1000)
  },

  PickerChange_status(e) {
    var that = this
    console.log(e);
    this.setData({
      index_status: parseInt(e.detail.value)
    })

    // order_status: _.lt(that.data.order_status_max),
    //   order_status: _.gt(that.data.order_status_min),
    console.log(this.data.picker[this.data.index_status])
    var cn_status = this.data.picker[this.data.index_status]
    // { { items.order_status == 0 ? "待支付" : "" } } { { items.order_status == 1 ? "已支付" : "" } } { { items.order_status == 2 ? "已配送" : "" } } { { items.order_status == 3 ? "退款中" : "" } } { { items.order_status == 4 ? "已退款" : "" } }
    if (cn_status == "全部") { //所有订单
      that.setData({
        order_status_max: 9999,
        order_status_min: -9999,
      })
    } else if (cn_status == "已支付") {
      that.setData({
        order_status_max: 2,
        order_status_min: 0,
      })
    } else if (cn_status == "待支付") {
      that.setData({
        order_status_max: 1,
        order_status_min: -1,
      })
    } else if (cn_status == "已配送") {
      that.setData({
        order_status_max: 3,
        order_status_min: 1,
      })
    } else if (cn_status == "退款中") {
      that.setData({
        order_status_max: 4,
        order_status_min: 2,
      })
    } else if (cn_status == "已退款") {
      that.setData({
        order_status_max: 5,
        order_status_min: 3,
      })
    }



  },
  PickerChange_type(e) {
    var that = this
    console.log(e);
    this.setData({
      index_type: parseInt(e.detail.value)
    })
    console.log(this.data.picker_type[this.data.index_type])
    var cn_type = this.data.picker_type[this.data.index_type]
    if (cn_type == "邮寄") {
      that.setData({
        order_type: "寄快递-免费上门",

      })
    } else if (cn_type == "代取") {
      that.setData({
        order_type: "^((?!寄快递-免费上门).)*$",

      })
    } else if (cn_type == "全部") {
      that.setData({
        order_type: ".",

      })
    }
  },


  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    // 获取完整的年月日 时分秒，以及默认显示的数组
    var obj = dateTimePicker.dateTimePicker(this.data.startYear, this.data.endYear);
    var obj1 = dateTimePicker.dateTimePicker(this.data.startYear, this.data.endYear);
    console.log(obj)
    // 精确到分的处理，将数组的秒去掉
    var lastArray = obj1.dateTimeArray.pop();
    var lastTime = obj1.dateTime.pop();

    this.setData({
      dateTime: obj.dateTime,
      dateTimeArray: obj.dateTimeArray,
      dateTimeArray1: obj1.dateTimeArray,
      dateTime1: obj1.dateTime,
      dateTimeArray2: obj1.dateTimeArray,
      dateTime2: obj1.dateTime,

     // ptinttime_begin: obj1.dateTimeArray[0][obj1.dateTime[0]] + "-" + obj1.dateTimeArray[1][obj1.dateTime[1]] + "-" + obj1.dateTimeArray[2][obj1.dateTime[2]] + " " + "00:00" ,
      ptinttime_begin: obj1.dateTimeArray[0][obj1.dateTime[0]] + "-" + obj1.dateTimeArray[1][obj1.dateTime[1]] + "-" + obj1.dateTimeArray[2][obj1.dateTime[2]] + " "  + obj1.dateTimeArray[3][obj1.dateTime[3]] + ":" + obj1.dateTimeArray[4][obj1.dateTime[4]],

     // ptinttime_begin_num: Date.parse((obj1.dateTimeArray[0][obj1.dateTime[0]] + "-" + obj1.dateTimeArray[1][obj1.dateTime[1]] + "-" + obj1.dateTimeArray[2][obj1.dateTime[2]] + " " +  "00:00"  ).replace(/-/g, '/')),

       ptinttime_begin_num: Date.parse((obj1.dateTimeArray[0][obj1.dateTime[0]] + "-" + obj1.dateTimeArray[1][obj1.dateTime[1]] + "-" + obj1.dateTimeArray[2][obj1.dateTime[2]] + " " + obj1.dateTimeArray[3][obj1.dateTime[3]] + ":" + obj1.dateTimeArray[4][obj1.dateTime[4]]).replace(/-/g, '/')),
      ptinttime_end: obj1.dateTimeArray[0][obj1.dateTime[0]] + "-" + obj1.dateTimeArray[1][obj1.dateTime[1]] + "-" + obj1.dateTimeArray[2][obj1.dateTime[2]] + " " + obj1.dateTimeArray[3][obj1.dateTime[3]] + ":" + obj1.dateTimeArray[4][obj1.dateTime[4]],
      ptinttime_end_num: Date.parse((obj1.dateTimeArray[0][obj1.dateTime[0]] + "-" + obj1.dateTimeArray[1][obj1.dateTime[1]] + "-" + obj1.dateTimeArray[2][obj1.dateTime[2]] + " " + obj1.dateTimeArray[3][obj1.dateTime[3]] + ":" + obj1.dateTimeArray[4][obj1.dateTime[4]]).replace(/-/g, '/'))
    });
    // 默认查询当天0点开始数据
    var dataname_m="dateTime1[3]"
    var dataname_s="dateTime1[4]"
    console.log(this.data.dateTime1[3])
    console.log(this.data.dateTime1)
    //   this.setData({
    //   [dataname_m]:0,
    //   [dataname_s]:0

    // })
if(wx.getStorageSync('ptinttime_end_num_next')&&wx.getStorageSync('dateTime_print_bgein_next')){
  this.setData({
    ptinttime_begin_num:wx.getStorageSync('ptinttime_end_num_next'),
    dateTime1:wx.getStorageSync('dateTime_print_bgein_next'),
    //  ptinttime_end_num_next  dateTime_print_bgein_next

  })
}
   



    var time = '2019-5-20 00:00';

    var repTime = time.replace(/-/g, '/'); //用正则主要是把“2019-05-20 00:00:00”转换成“2019/05/0 00:00:00”兼容ios
    // Date.parse(time.replace(/-/g, '/'));

    console.log("返回时间：" + repTime);

    var timeTamp = Date.parse(repTime);

    console.log("返回时间戳：" + timeTamp)

  },
  changeDate(e) {
    this.setData({
      date: e.detail.value
    });
  },
  changeTime(e) {
    this.setData({
      time: e.detail.value
    });
  },
  changeDateTime(e) {
    this.setData({
      dateTime: e.detail.value
    });
  },
  changeDateTime1(e) {
    console.log(e)
    console.log(e.detail.value)
    // var 
    this.setData({
      dateTime1: e.detail.value,
      // dateTime1_begin:  dateTimeArray1[0][dateTime1[0]]  -   dateTimeArray1[1][dateTime1[1]] - dateTimeArray1[2][dateTime1[2]]  dateTimeArray1[3][dateTime1[3]]  :   dateTimeArray1[4][dateTime1[4]] }}

    });
  },
  changeDateTime2(e) {
    this.setData({
      dateTime2: e.detail.value
    });
  },
  changeDateTimeColumn(e) {
    var arr = this.data.dateTime,
      dateArr = this.data.dateTimeArray;

    arr[e.detail.column] = e.detail.value;
    dateArr[2] = dateTimePicker.getMonthDay(dateArr[0][arr[0]], dateArr[1][arr[1]]);

    this.setData({
      dateTimeArray: dateArr,
      dateTime: arr
    });
  },
  changeDateTimeColumn1(e) {
    console.log(e)
    var arr = this.data.dateTime1,
      dateArr = this.data.dateTimeArray1;

    arr[e.detail.column] = e.detail.value;
    dateArr[2] = dateTimePicker.getMonthDay(dateArr[0][arr[0]], dateArr[1][arr[1]]);

    this.setData({
      dateTimeArray1: dateArr,
      dateTime1: arr,
      ptinttime_begin: dateArr[0][arr[0]] + "-" + dateArr[1][arr[1]] + "-" + dateArr[2][arr[2]] + " " + dateArr[3][arr[3]] + ":" + dateArr[4][arr[4]],
      ptinttime_begin_num: Date.parse((dateArr[0][arr[0]] + "-" + dateArr[1][arr[1]] + "-" + dateArr[2][arr[2]] + " " + dateArr[3][arr[3]] + ":" + dateArr[4][arr[4]]).replace(/-/g, '/'))


    });
    console.log(dateArr)
    console.log(
      dateArr[0][arr[0]] + "-" + dateArr[1][arr[1]] + "-" + dateArr[2][arr[2]] + " " + dateArr[3][arr[3]] + ":" + dateArr[4][arr[4]]

    )
  },
  changeDateTimeColumn2(e) {
    var arr = this.data.dateTime2,
      dateArr = this.data.dateTimeArray2;

    arr[e.detail.column] = e.detail.value;
    dateArr[2] = dateTimePicker.getMonthDay(dateArr[0][arr[0]], dateArr[1][arr[1]]);

    this.setData({
      dateTimeArray2: dateArr,
      dateTime2: arr,
      ptinttime_end: dateArr[0][arr[0]] + "-" + dateArr[1][arr[1]] + "-" + dateArr[2][arr[2]] + " " + dateArr[3][arr[3]] + ":" + dateArr[4][arr[4]],
      ptinttime_end_num: Date.parse((dateArr[0][arr[0]] + "-" + dateArr[1][arr[1]] + "-" + dateArr[2][arr[2]] + " " + dateArr[3][arr[3]] + ":" + dateArr[4][arr[4]]).replace(/-/g, '/'))
    });
    console.log(
      dateArr[0][arr[0]] + "-" + dateArr[1][arr[1]] + "-" + dateArr[2][arr[2]] + " " + dateArr[3][arr[3]] + ":" + dateArr[4][arr[4]]

    )
  },

  // 打电话
  phonecall(e) {
    console.log(e)
    var phone_no = e.currentTarget.dataset.addressee_phoneno
    console.log(phone_no)
    wx.makePhoneCall({
      phoneNumber: phone_no,
    })
  },
  TimeChange(e) {
    console.log(e)
    this.setData({
      time: e.detail.value
    })
  },
  DateChange(e) {
    console.log(e)

    this.setData({
      date: e.detail.value
    })
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },


  //   async function() {
  //     for...{
  //       let res1 = await promise
  //        let res2 = await promise
  //     }
  // }

  for_100_sure(res) {
    var that=this
    wx.showModal({
       title: '注意',
      content: '确认是否打印？\r\n 不要点错了浪费纸张！！！ ',
      showCancel: true, //是否显示取消按钮
      cancelText: "不必", //默认是“取消”
      cancelColor: '#b9b9b9', //取消文字的颜色
      confirmText: "好的", //默认是“确定”
      confirmColor: '#000000', //确定文字的颜色
      success: function (res) {
        if (res.cancel) {
          //点击取消,默认隐藏弹框
          console.log("用户取消选择！")
        } else {
          that.for_100()
        }
      }
    })
  },


  for_100(res) {
    var that = this
    wx.setStorage({
      data: that.data.ptinttime_end_num,
      key: 'ptinttime_end_num_next',
    })
    wx.setStorage({
      data: that.data.dateTime2,
      key: 'dateTime_print_bgein_next',
    })
    wx.setStorage({
      data: that.data.dateTime,
      key: 'dateTime',
    })
    if (that.data.ptinttime_begin_num >= that.data.ptinttime_end_num) {
      wx.showToast({
        title: '请注意时间范围选择是否正确!',
        icon: "none"
      })
      return

    }
    wx.showLoading({
      title: '打印中请稍等...',
      mask: true
    })
    // 首页输出打印时间
    // that.Opoint_prrint_Interface("打印时间:" + that.data.ptinttime_begin + "至" + that.data.ptinttime_end)
    that.setData({
      // 重置打印数zu
      oder_lists_print: []
    });
    // 设置打印日志
    var date_now = Date.now()
    var print_logs = wx.getStorageSync('print_logs') || []
    print_logs.unshift(date_now)
    wx.setStorageSync('print_logs', print_logs)
    wx.setStorageSync('print_logs_date_now', date_now)

    const db = wx.cloud.database()
    const _ = db.command

    wx.removeStorageSync("oder_Lists_total")
    db.collection('oder_Lists').where({
      // _openid: 'xxx' // 填入当前用户 openid
      //  条件：与查询详细必须信息一致
      timestamp: _.lte(that.data.ptinttime_end_num).gte(that.data.ptinttime_begin_num),
      //      timestamp: command.gte(that.data.ptinttime_begin_num),

      // order_status: 1
      order_status: _.lt(that.data.order_status_max).gt(that.data.order_status_min),
      express_company: db.RegExp({
        regexp: that.data.order_type,
        options: 'i',
      })
    }).count({
      success: function (res) {
        console.log(res)

        console.log(res.total)
        wx.setStorageSync('oder_Lists_total', res.total)
        that.setData({
          oder_Lists_total: res.total
        })
        // 首页输出打印时间
        that.Opoint_prrint_Interface("打印时间:" + that.data.ptinttime_begin + "至" + that.data.ptinttime_end + "    订单合计:" + res.total + "单.")
        console.log(res.total / 20)

        //var page = Math.floor(res.total/20)
        var page = Math.ceil(res.total / 20)

        console.log(page)
        that.get_oder_Lists(page)

      }
    })


  },
  search_oder_lists() {
    var that = this
    if (that.data.ptinttime_begin_num >= that.data.ptinttime_end_num) {
      wx.showToast({
        title: '请注意时间范围选择是否正确!',
        icon: "none"
      })
      return

    }
    wx.showLoading({
      title: '查询中请稍等...',
      mask: true
    })
    // 首页输出打印时间
    // that.Opoint_prrint_Interface("打印时间:" + that.data.ptinttime_begin + "至" + that.data.ptinttime_end)
    var that = this
    that.setData({
      // 重置打印数zu
      oder_lists_print: []
    });
    // 设置打印日志
    var date_now = Date.now()
    var print_logs = wx.getStorageSync('print_logs') || []
    print_logs.unshift(date_now)
    wx.setStorageSync('print_logs', print_logs)
    wx.setStorageSync('print_logs_date_now', date_now)

    const db = wx.cloud.database()
    const command = db.command
    const _ = db.command

    wx.removeStorageSync("oder_Lists_total")
    var e = 'post'
    db.collection('oder_Lists').where({
      // _openid: 'xxx' // 填入当前用户 openid
      //  条件：与查询详细必须信息一致
      timestamp: command.lte(that.data.ptinttime_end_num).gte(that.data.ptinttime_begin_num),
      // timestamp: command.gte(that.data.ptinttime_begin_num),

      //order_status: that.data.index_status,
      order_status: _.lt(that.data.order_status_max).gt(that.data.order_status_min),

      express_company: db.RegExp({
        regexp: that.data.order_type,
        options: 'i',
      })

      // idcard_no: db.RegExp({
      //   regexp: "555",
      //   options: 'i',
      // })

    }).count({
      success: function (res) {
        console.log(res)
        //return

        console.log(res.total)
        that.setData({
          oder_Lists_total: res.total
        })
        wx.setStorageSync('oder_Lists_total', res.total)
        console.log(res.total / 20)

        //var page = Math.floor(res.total/20)
        var page = Math.ceil(res.total / 20)

        console.log(page)
        that.get_oder_Lists_search(page)

      }
    })
  },
  async get_oder_Lists_2(page) {
    var that = this
    let aa = ''
    for (let index = 0; index < page; index++) {
      console.log(index)
      console.log(page)

      aa = await (that.find_lists_2(index, page))
    }
    return aa

  },
  get_oder_Lists(page) {
    var that = this
    setTimeout(
      that.find_lists_2(that.data.index, page), 500)

  },
  get_oder_Lists_search(page) {
    var that = this
    setTimeout(
      that.find_lists_search(that.data.index, page), 500)

  },

  find_lists_search(index, page) {
    var that = this

    const db = wx.cloud.database()

    const _ = db.command
    // let res1 = await promise
    db.collection('oder_Lists').where({
        timestamp: _.lte(that.data.ptinttime_end_num).gte(that.data.ptinttime_begin_num),
        //order_status: 0,
        order_status: _.lt(that.data.order_status_max).gt(that.data.order_status_min),
        express_company: db.RegExp({
          regexp: that.data.order_type,
          options: 'i',
        })

        // order_status: db.RegExp({
        //   regexp: '',
        //   options: 'i',
        // })

      }).field({

      })
      .orderBy('shopname', 'desc')

      .orderBy('express_company', 'desc')

      .orderBy('timestamp', 'desc')
      .skip(index * 20)
      .limit(20)
      .get({
        success(res) {
          console.log(res)
          that.setData({
            oder_lists_print: that.data.oder_lists_print.concat(res.data)
          });
          // that.words_to_content(that.data.oder_lists_print)
          if (page == that.data.index) {
            console.log("达到循环终点")
            console.log("that.words_to_content(that.data.")
            that.setData({
              index: 0
            })
            //   that.words_to_content(that.data.oder_lists_print)
            console.log("11111111111111111")
            console.log(that.data.oder_lists_print)
            that.setData({
              oder_lists: that.data.oder_lists_print
            })
            //  that.words_to_content_2(that.data.oder_lists_print)
            wx.hideLoading()

          } else {
            // console.log(page)
            // console.log(index)

            console.log("未达到循环终点")
            that.setData({
              index: that.data.index + 1
            })
            that.find_lists_search(that.data.index, page)
          }
          //return "ok"

          // return res1


        },
        fail: console.error
      })


  },


  find_lists_2(index, page) {
    var that = this

    const db = wx.cloud.database()

    const _ = db.command
    // let res1 = await promise
    db.collection('oder_Lists').where({
        timestamp: _.lte(that.data.ptinttime_end_num).gte(that.data.ptinttime_begin_num),
        express_company: db.RegExp({
          regexp: that.data.order_type,
          options: 'i',
        }),

        //  order_status: 1,
        order_status: _.lt(that.data.order_status_max).gt(that.data.order_status_min),

        //    order_status: db.RegExp({
        //   regexp: '',
        //   options: 'i',
        // })

      }).field({

      })
      .orderBy('shopname', 'desc')

      .orderBy('express_company', 'desc')

      .orderBy('timestamp', 'desc')
      .skip(index * 20)
      .limit(20)
      .get({
        success(res) {
          console.log(res)
          that.setData({
            oder_lists_print: that.data.oder_lists_print.concat(res.data)
          });
          // that.words_to_content(that.data.oder_lists_print)
          if (page == that.data.index) {
            console.log("达到循环终点")
            console.log("that.words_to_content(that.data.")
            that.setData({
              index: 0
            })
            //   that.words_to_content(that.data.oder_lists_print)
            console.log("11111111111111111")
            console.log(that.data.oder_lists_print)

            that.words_to_content_2(that.data.oder_lists_print)


          } else {
            // console.log(page)
            // console.log(index)

            console.log("未达到循环终点")
            that.setData({
              index: that.data.index + 1
            })
            that.find_lists_2(that.data.index, page)
          }
          //return "ok"

          // return res1


        },
        fail: console.error
      })


  },

  async find_lists(index, page) {
    var that = this

    const db = await wx.cloud.database()

    const _ = db.command
    // let res1 = await promise
    await (db.collection('oder_Lists').where({
        timestamp: _.lte(that.data.ptinttime_end_num),
        timestamp: _.gte(that.data.ptinttime_begin_num),
        // order_status: 1,
        order_status: _.lt(that.data.order_status_max).gt(that.data.order_status_min),

        // order_status: db.RegExp({
        //   regexp: '',
        //   options: 'i',
        // })

      }).field({
        // oder_time: true,

        // express_company: true,
        // // addressee_phoneno: true,
        // addressee: true,
        // // oder_no: true,
        // oder_time: true,
        // timestamp: true,

        // 只返回 tags 数组前 3 个元素
        // tags: db.command.project.slice(3),
      })
      .orderBy('shopname', 'desc')

      .orderBy('express_company', 'desc')

      .orderBy('timestamp', 'desc')
      .skip(index * 20)
      .limit(20)
      .get({
        success(res) {
          console.log(res)
          // that.setData({
          //   oder_lists_print: that.data.oder_lists_print.concat(res.data)
          // });
          // that.words_to_content(that.data.oder_lists_print)
          if (page == index) {
            console.log("达到循环终点")
            console.log("that.words_to_content(that.data.")

            // that.words_to_content(that.data.oder_lists_print)

          } else {
            // console.log(page)
            // console.log(index)

            console.log("未达到循环终点")
          }

          return res1


        },
        fail: console.error
      }))
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    var that = this

    const db = wx.cloud.database()
    var _ = db.command
    db.collection("oder_Lists").where({
        // _openid: app.globalData.openid
        //_openid: wx.getStorageSync('openid')
        //  order_status: 1,
        order_status: _.lt(that.data.order_status_max),
        order_status: _.gt(that.data.order_status_min),
        //    order_status: db.RegExp({
        //   regexp: '',
        //   options: 'i',
        // })
      }).orderBy('timestamp', 'desc')
      .limit(20)
      // 暂时限制30个后期优化做分页
      .get({
        success(res) {
          console.log(res)
          console.log(res)

          if (res.data.length > 0) {
            that.setData({
              oder_lists: res.data
              // _id: res.data[0]._id,
              // num: res.data[0].status
            })


          }
          console.log(res)
        },
        fail(res) {
          console.log(res)

        },
        complete(res) {
          console.log(res)
          // 关闭加载中！
          wx.hideLoading()
        }
      })




  },

  words_to_content(res) {
    var that = this
    var order_content = that.data.order_content;
    //console.log(res)
    //return 
    var order_content = ""
    for (let index in res) {
      order_content = order_content + "<left><bold>"
      order_content = order_content + res[index].express_company
      order_content = order_content + "</bold></left>"
      order_content = order_content + "<left><bold>"
      order_content = order_content + "No." + (parseInt(index) + 1)

      order_content = order_content + "["
      order_content = order_content + (res[index].building_number ? res[index].building_number : res[index].Alternate_address.detailInfo)
      //order_content = order_content + "测试楼"
      order_content = order_content + "] 姓名:"
      order_content = order_content + res[index].addressee
      order_content = order_content + "</bold></left>"
      order_content = order_content + "<left><bold>"
      order_content = order_content + "取件码:"
      order_content = order_content + res[index].verification_code
      order_content = order_content + " "
      order_content = order_content + "手机:"
      order_content = order_content + res[index].addressee_phoneno
      order_content = order_content + "</bold></left>"
      // console.log(index)
      // console.log(res.length)

      // console.log(order_content)

      if (index > 0 && index % 5 == 0) {
        console.log(order_content)

        //that.Opoint_prrint_Interface(order_content)
        // console.log(index + "是9的倍数");
        // console.log(order_content)
        // var key = "order_content_" + index
        // wx.setStorageSync(key, order_content)
        // try {
        //   var value = wx.getStorageSync(key)
        //   if (value) {
        //     console.log(value)
        //     console.log(key + "数据存在")
        //     //that.Opoint_prrint_Interface(order_content)
        //     // Do something with return value
        //     wx.removeStorageSync(key)
        //   }
        // } catch (e) {
        //   // Do something when catch error
        // }
        // // that.setData({
        // //   order_content:""
        // // })
        // 清零
        var order_content = ""


      } else {
        console.log(index + "不是5的倍数");
        console.log(res.length)

        if (parseInt(index) + 1 == res.length) {
          console.log("循环结束")
          // 调用打印
          //console.log(order_content)

          // return
          // that.Opoint_prrint_Interface(order_content)
        }
      }
      // if (parseInt(index) + 1 == res.length) {
      //   console.log("循环结束")
      //   // 调用打印
      //   console.log(order_content)

      //   // return
      //   // that.Opoint_prrint_Interface(order_content)
      // } else {
      //   console.log("循环未结束")

      // }


    }
  },

  words_to_content_2(res) {
    if(res.length==0){
      wx.showLoading({
        title: '没有订单！',
        mask: true
      })
      setTimeout(function () {
        wx.hideLoading()
      }, 1500)
      return
    }
    var that = this
    var order_content = that.data.order_content;
    //console.log(res)
    //return 
    //var order_content_lists=[]
    that.setData({
      order_content_lists: []
    })
    var order_content = ""
    for (let index in res) {
      order_content = order_content + "<left><bold>"
      order_content = order_content + res[index].express_company
      order_content = order_content + "</bold></left>"
      order_content = order_content + "<left><bold>"
      order_content = order_content + "No." + (parseInt(index) + 1)

      order_content = order_content + "["
      order_content = order_content + (res[index].building_number ? res[index].building_number : res[index].Alternate_address.detailInfo)
      //order_content = order_content + "测试楼"
      order_content = order_content + "] 姓名:"
      order_content = order_content + res[index].addressee
      order_content = order_content + "</bold></left>"
      order_content = order_content + "<left><bold>"
      order_content = order_content + "取件码:"
      order_content = order_content + res[index].verification_code
      order_content = order_content + " "
      order_content = order_content + "手机:"
      order_content = order_content + res[index].addressee_phoneno
      order_content = order_content + "</bold></left>"
      // console.log(index)
      // console.log(res.length)

      // console.log(order_content)

      if (index > 0 && index % 10 == 0) {
        console.log(order_content)

        that.data.order_content_lists.push(order_content)


        var order_content = ""
        if (parseInt(index) + 1 == res.length) {
          console.log("循环结束")
          // 调用打印
          console.log(order_content)
          that.data.order_content_lists.push(order_content)
          console.log(2333333333333333333333)
          // return
          // that.Opoint_prrint_Interface(order_content)

          // that.for_Opoint_prrint_Interface(that.data.order_content_lists)
          setTimeout(function () {
            that.for_Opoint_prrint_Interface(that.data.order_content_lists)
          }, 500)
          console.log(that.data.order_content_lists)
        }

      } else {
        console.log(index + "不是10的倍数");
        console.log(res.length)

        if (parseInt(index) + 1 == res.length) {
          console.log("循环结束")
          // 调用打印
          console.log(order_content)
          that.data.order_content_lists.push(order_content)
          console.log(2333333333333333333333)
          // return
          // that.Opoint_prrint_Interface(order_content)

          // that.for_Opoint_prrint_Interface(that.data.order_content_lists)
          setTimeout(function () {
            that.for_Opoint_prrint_Interface(that.data.order_content_lists)
          }, 500)
          console.log(that.data.order_content_lists)
        }
      }



    }
  },
  for_Opoint_prrint_Interface(res) {
    var that = this;

    var order_content_lists = res
    console.log(order_content_lists)
    console.log(order_content_lists.length)
    //return
    var times = 0
    var i = setInterval(function (order_content_lists) {
      // console.log(that.data.order_content_lists)
      // times++
      if (times >= that.data.order_content_lists.length) {
        console.log("循环结束")

        // 打印结束
        wx.showLoading({
          title: '打印完成！',
          mask: true
        })
        setTimeout(function () {
          wx.hideLoading()
        }, 1500)

        clearInterval(i)
        times++
      } else {
        console.log(times)
        // console.log(that.data.order_content_lists[0])
        // console.log(that.data.order_content_lists[1])
        // console.log(that.data.order_content_lists[2])

        console.log(that.data.order_content_lists[times])
        that.Opoint_prrint_Interface(that.data.order_content_lists[times])
        times++
      }

    }, 1000)

  },



  Opoint_prrint_endnow(res) {
    var that = this

    const db = wx.cloud.database()
    db.collection('todos').where({
      _openid: 'xxx' // 填入当前用户 openid
    }).count()


    db.collection("oder_Lists").where({
        // _openid: app.globalData.openid
        //_openid: wx.getStorageSync('openid')

        //  order_status: 1,

        order_status: _.lt(that.data.order_status_max),
        order_status: _.gt(that.data.order_status_min),


      }).orderBy('timestamp', 'desc')
      .skip(0)
      .limit(20)
      // 暂时限制30个后期优化做分页
      .get({
        success(res) {
          console.log(res)
          console.log(res)

          if (res.data.length > 0) {
            that.setData({
              oder_lists: res.data
              // _id: res.data[0]._id,
              // num: res.data[0].status
            })
            // 循环拼接字段
            // < left > <bold>【东XXX西XXX】 ***1223 ADC23123456 电话 135***  </bold></left >
            //   <left><bold>【东XXX西XXX】 ***1223 ADC23123456 电话 135***  </bold></left>
            var order_content = '';

            for (var index in res.data) {
              order_content = order_content + "<left><bold>"
              // order_content = order_content + index

              order_content = order_content + "["
              // order_content = order_content + (res.data[index].building_number ? res.data[index].building_number : res.data[index].Alternate_address.detailInfo)
              order_content = order_content + "测试楼"
              order_content = order_content + "] 姓名:"
              order_content = order_content + res.data[index].addressee
              order_content = order_content + "</bold></left>"
              order_content = order_content + "<left><bold>"
              order_content = order_content + "取件码:"
              order_content = order_content + res.data[index].verification_code
              order_content = order_content + " "
              order_content = order_content + "手机:"
              order_content = order_content + res.data[index].addressee_phoneno
              order_content = order_content + "</bold></left>"
              console.log(index)
              console.log(order_content)

            }
            // order_content = order_content+"<left><bold>"
            // order_content = order_content + "【"
            // order_content = order_content + (res.data[0].building_number ? res.data[0].building_number : res.data[0].Alternate_address.detailInfo)
            // order_content = order_content + "】  "
            // order_content = order_content + res.data[0].addressee
            // order_content = order_content + " "

            // order_content = order_content + res.data[0].verification_code
            // order_content = order_content + " "
            // order_content = order_content + "手机："
            // order_content = order_content + res.data[0].addressee_phoneno
            // order_content = order_content + " </bold></left>"

            console.log(order_content)
            // 调用打印

            // return
            that.Opoint_prrint_Interface(order_content)

          }
          console.log(res)
        },
        fail(res) {
          console.log(res)

        },
        complete(res) {
          console.log(res)
          // 关闭加载中！
          wx.hideLoading()
        }
      })





  },



  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
    var that = this
    that.onShow()
    wx.stopPullDownRefresh()
    //wx.stopPullDownRefresh()
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
    console.log("触底")
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  },
  Opoint_prrint(res) {
    // secret：接口密钥(必需)

    // print_sn：打印机SN码(必需)

    // print_num：打印联数(必需)

    // order_content：打印内容(必需)

    // order_id：备注单号(打印完成后回调的订单标识，没有请填0)

    // notify_url：回调url(可不传, 若不传则以商户配置的url为准)

    wx.request({
      url: 'https://www.yprinter.com/api/Order/addOrder',
      data: {
        secret: "***", //接口密钥(必需)
        print_sn: "***", //打印机SN码(必需)
        print_num: 1, //打印联数(必需)
        order_content: "<middle><dh><dw><bold>顺丰</bold></dw></dh></middle> <middle><dh><dw><bold>顺丰</bold></dw></dh></middle> <middle><dh><dw><bold>顺丰</bold></dw></dh></middle> <left><dh><dw><bold>顺丰</bold></dw></dh></left> <left><bold>【东899】</bold></left> <left>***1234 ADC231568 135***</left> <left><bold>【东899】</bold></left> <left>***1234 ADC231568 135***</left> <left><bold>【东899】</bold></left> <left>***1234 ADC231568 135***</left> <left><bold>【东899】</bold></left> <left>***1234 ADC231568 135***</left> <left><dh><dw><bold>顺丰</bold></dw></dh></left> <left><bold>【东9152】</bold></left> <left>***123456 ADC2312345 </left> <left>电话 135***</left> <left><bold>【东9152】</bold></left> <left>***123456 ADC2312345 </left> <left>电话 135***</left> <left><bold>【东9152】</bold></left> <left>***123456 ADC2312345 </left> <left>电话 135***</left> <left><bold>【东9152】</bold></left> <left>***123456 ADC2312345 </left> <left>电话 135***</left> <left><dh><dw><bold>顺丰</bold></dw></dh></left> <left><bold>【东XXX西XXX】 ***1223 ADC23123456 电话 135***  </bold></left> <left><bold>【东XXX西XXX】 ***1223 ADC23123456 电话 135***  </bold></left> <left><bold>【东XXX西XXX】 ***1223 ADC23123456 电话 135***  </bold></left> <left><bold>【东XXX西XXX】 ***1223 ADC23123456 电话 135***  </bold></left> <left><bold>【东XXX西XXX】 ***1223 ADC23123456 电话 135***  </bold></left>", //打印内容(必需)

        order_id: 0, //备注单号(打印完成后回调的订单标识，没有请填0)

        notify_url: "", //回调url(可不传, 若不传则以商户配置的url为准)

      },
      header: {
        "Content-Type": "application/x-www-form-urlencoded"
      },
      method: 'GET',
      success: function (res) {
        console.log(res);


      },
      fail: function (res) {
        console.log(res)
      },
      complete(res) {
        console.log(res)

      }
    });
  },
  // 零点打印接口
  Opoint_prrint_Interface(order_content) {
    // console.log(111)
    // console.log(res)
    // secret：接口密钥(必需)

    // print_sn：打印机SN码(必需)

    // print_num：打印联数(必需)

    // order_content：打印内容(必需)

    // order_id：备注单号(打印完成后回调的订单标识，没有请填0)

    // notify_url：回调url(可不传, 若不传则以商户配置的url为准)

    wx.request({

      url: 'https://www.yprinter.com/api/Order/addOrder',


      data: {
        secret: "***", //接口密钥(必需)

        print_sn: "***", //打印机SN码(必需)

        print_num: 1, //打印联数(必需)

        order_content: order_content, //打印内容(必需)

        //order_id: 0, //备注单号(打印完成后回调的订单标识，没有请填0)
        order_id: parseInt(Math.random() * 100000000), //备注单号(打印完成后回调的订单标识，没有请填0)

        notify_url: "", //回调url(可不传, 若不传则以商户配置的url为准)

      },
      header: {
        "Content-Type": "application/x-www-form-urlencoded"
      },
      method: 'GET',
      success: function (res) {
        console.log(res);
        // wx.showLoading({
        //   title: '加入打印队列...',
        //   mask:true
        // })
        // wx.showToast({
        //   title: '已成功加入打印队列！',
        // })

      },
      fail: function (res) {
        console.log(res)
      },
      complete(res) {
        console.log(res)
        // wx.hideLoading()

      }
    });
  }



})